package cn.com.nes.site.interceptor;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

/**
 * 请求封装
 * 解决日志切面，获取请求body后，重新写入body中
 */
@Slf4j
public class RequestWrapper extends HttpServletRequestWrapper {

    // private final String body;

    /**
     * 将InputStream中的字节保存到ByteArrayOutputStream中。
     */
    private ByteArrayOutputStream byteArrayOutputStream = null;

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        inputStreamCacher(request.getInputStream());
    }

    public void inputStreamCacher(InputStream inputStream) {
        if (ObjectUtil.isNull(inputStream)) {
            return;
        }

        byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buffer)) > -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            byteArrayOutputStream.flush();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ServletInputStream servletInputStream = new ServletInputStream() {

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }

            // @Override
            // public int read(byte[] b, int off, int len) throws IOException {
            //     return byteArrayInputStream.read(b, off, len);
            // }

            @Override
            public long skip(long n) throws IOException {
                return byteArrayInputStream.skip(n);
            }

            @Override
            public int available() throws IOException {
                return byteArrayInputStream.available();
            }

            @Override
            public void mark(int readlimit) {
                byteArrayInputStream.mark(readlimit);
            }

            @Override
            public void reset() throws IOException {
                byteArrayInputStream.reset();
            }
        };
        return servletInputStream;

    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

    public String getBody() throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        ServletInputStream inputStream = this.getInputStream();
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        return new String(os.toByteArray(), "UTF8");
    }

}
